package itbour.onetouchshow.base;

/**
 * Created by Yang on 2018/2/28.
 */

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.andview.refreshview.XRefreshView;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.base.mvp.BaseView;
import itbour.onetouchshow.base.mvp.MVPBaseFragment;
import itbour.onetouchshow.utils.UIUtils;

public abstract class BaseRefreshFragmentV_1_3<V extends BaseView, T extends BasePresenterImpl<V>> extends MVPBaseFragment<V, T> implements BaseView {

    public static final int ON_REFRESH = 1;
    public static final int ON_LOAD = 2;
    @BindView(R.id.recyclerview)
    public RecyclerView recyclerView;
    @BindView(R.id.xrefreshview)
    public XRefreshView xRefreshView;
    public int mPageIndex = 0;
    public int pullStatus;
    public BaseRefreshAdapter baseRefreshAdapter;
    @BindView(R.id.rl_search_no_back)
    RelativeLayout searchLayout;
    @BindView(R.id.ll_search_layout_container)
    LinearLayout searchLayoutContainer;
    @BindView(R.id.iv_personal_create)
    ImageView ivPersonalCreate;
    @BindView(R.id.v_status_bar)
    View vStatusBar;
    private View noNetView;

    public View getvStatusBar() {
        return vStatusBar;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.base_refresh_fragment_v_1_3;
    }

    public RelativeLayout getSearchLayout() {
        return searchLayout;
    }

    public LinearLayout getSearchLayoutContainer() {
        return searchLayoutContainer;
    }

    public ImageView getIvPersonalCreate() {
        return ivPersonalCreate;
    }

    @Override
    protected void initView() {
        // 设置是否可以下拉刷新
        xRefreshView.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(false);
        // 设置上次刷新的时间
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        //refreshView.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        //xRefreshView.setAutoRefresh(true);
        xRefreshView.setAutoLoadMore(true);
        //xRefreshView.setPinnedTime(1000);
        xRefreshView.stopLoadMore(false);
        xRefreshView.setMoveForHorizontal(true);
        CustomGifHeader header = new CustomGifHeader(getContext());
        xRefreshView.setCustomHeaderView(header);
        xRefreshView.enableEmptyView(false);
        //xRefreshView.setMoveHeadWhenDisablePullRefresh(false);
        xRefreshView.setMoveFootWhenDisablePullLoadMore(false);
        noNetView = UIUtils.inflate(R.layout.rv_no_network_view);
        xRefreshView.setEmptyView(noNetView);
        // xRefreshView.getEmptyView().setVisibility(View.GONE);


        initRecycleView();


    }

    public void initRecycleView() {
        baseRefreshAdapter = getBaseRefreshAdapter();
        if (baseRefreshAdapter == null) {
            return;
        }
        //baseRefreshAdapter.setCustomLoadMoreView(new XRefreshViewFooter(getContext()));
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()) {
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        });
        baseRefreshAdapter.setHeaderView(getHeadView(), recyclerView);
        recyclerView.setAdapter(baseRefreshAdapter);

    }


    public abstract BaseRefreshAdapter getBaseRefreshAdapter();

    public abstract View getHeadView();

    public void showXRefreshViewNetWorkErrorView() {
        if (pullStatus == ON_LOAD) {
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            xRefreshView.stopRefresh();
        }
        noNetView.findViewById(R.id.error_view).setVisibility(View.VISIBLE);
        if (xRefreshView != null) {
            xRefreshView.enableEmptyView(true);
        }

    }


    public void stopXfresh() {
        if (pullStatus == ON_LOAD) {
            xRefreshView.stopLoadMore();
        } else if (pullStatus == ON_REFRESH) {
            xRefreshView.stopRefresh();
        }
    }

    public void showXRefreshViewNetWorkSuccessView() {
        if (noNetView != null) {
            noNetView.findViewById(R.id.error_view).setVisibility(View.GONE);
        }
        if (xRefreshView != null) {
            xRefreshView.enableEmptyView(false);
        }

    }

}
